package com.zc.im.connector.init;

import com.zc.im.connector.handler.HeartBeatServerHandler;
import com.zc.im.netty.codec.ImDecoder;
import com.zc.im.netty.codec.ImEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Project：im
 * Date：2022/4/21
 * Time：14:17
 * Description：im的channel初始化器
 *
 * @author Challen.Zhang
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class ImChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final HeartBeatServerHandler heartBeatServerHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        //对象传输处理[解码]
        ch.pipeline().addLast(new ImDecoder());

        ch.pipeline().addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
        ch.pipeline().addLast(heartBeatServerHandler);

        //对象传输处理[编码]
        ch.pipeline().addLast(new ImEncoder());
    }
}
